package com.yvan.eventsourcing;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;

/**
 * Created by Administrator on 2018/12/12.
 */
public class EventContainerFactoryBeanBuilder {

    public static EventContainerFactoryBean build(EventConfig eventConfig) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(eventConfig.getRabbitmq().getHost());
        connectionFactory.setPort(eventConfig.getRabbitmq().getPort());
        connectionFactory.setUsername(eventConfig.getRabbitmq().getUsername());
        connectionFactory.setPassword(eventConfig.getRabbitmq().getPassword());
        connectionFactory.setVirtualHost(eventConfig.getRabbitmq().getVhost());

        EventContainerFactoryBean factory = new EventContainerFactoryBean();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setExchange(eventConfig.getExchangeName());
        factory.setErrorExchange(eventConfig.getErrorExchangeName());
        factory.setVersion(eventConfig.getVersion());
        return factory;
    }
}
